<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Scout\Searchable;

class Post extends Model
{
    use Searchable,SoftDeletes;

    
    // whereCate 获取指定分类案例
    public function scopeWhereCate($query, $cateid)
    {
        if($cateid) return $query->where('category_id',$cateid);
        return $query;
    }
    
    public function user()  //这个评论是评论谁的评论的 引用谁的评论
    {
         return $this->belongsTo(\App\User::class);
    }
    
    public function category()  //这个评论是评论谁的评论的 引用谁的评论
    {
         return $this->belongsTo(Category::class);
    }

    
    public function reports() //这个帖子的举报记录
    {
        return $this->morphMany(Report::class, 'reportable');
    }
    
    public function setImagesAttribute($images)
    {
        if (is_array($images)) {
            $this->attributes['images'] = json_encode($images);
        }
    }

    public function getImagesAttribute($images)
    {
        return json_decode($images, true);
    }

    
    /**
     * Get the indexable data array for the model.
     *
     * @return array
     */
    public function toSearchableArray()
    {
        return [
            'body'=>$this->body,
        ];
    }
}
